package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrdersettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.entity.Member;
import com.itheima.health.pojo.entity.Order;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    //检查预约日期是否进行提前预约设置，如果没有则无法预约
    //体检预约
    @Override
    public Order order(Map map){
        //检查预约日期是否进行提前预约设置，如果没有则无法预约
        String orderDate=(String) map.get("orderDate");//预约日期
//        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DataUtils.StrToDate(orderDate));
//        if (orderSetting == null){
//            throw new BusinessRuntimeException("已经提前预约");
//        }
//        //检查用户所选择的预约日期是否已经约满，如果约满则无法与预约
//        int number =orderSetting.getNumber();//可预约人数
//        int reservations = orderSetting.getReservations();//已预约人数
//        if (reservations>number){
//            throw new BusinessRuntimeException("预约日期已满");
//        }
        //检查用户是否重复预约（同个用户在同一天预约同一个套擦），如果重复预约则无法完成再次预约
        String telephone = (String) map.get("telephone");//获取用户输入的手机号
        Member member = memberDao.findByTelephone(telephone);
        if (member!=null){
            //判断是否重复预约
            Integer memberId = member.getId();
            Date order_Date = DataUtils.StrToDate(orderDate);//预约日期
            String setmealId =(String) map.get("setmealId");//套餐id
            Order order = new Order(memberId,order_Date,Integer.parseInt(setmealId));
            //根据条件查询
            List<Order>list=orderDao.findByCondition(order);
            if (list!=null &&list.isEmpty()){
                //说明重复预约，无法完成再次预约
                throw new BusinessRuntimeException("重复预约");
            }
        }else { //判断用户是否是会员，如果是会员则完成预约，不是则完成注册并预约
            member =new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);}
        //预约成功，更新当日的已预约人数
        Order order =new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DataUtils.StrToDate(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType("微信预约");
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

//        orderSetting.setReservations(orderSetting.getReservations()+1);//设置已预约人数+1
//        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.selectById(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DataUtils.DateToStr(orderDate));
        }
        return map;
    }
}

